<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Members extends CI_Controller {

    var $member_id;
    var $aktifasi;
    var $cari;
    var $message;

    function __construct() {
        parent::__construct();
        $this->load->library('auth');
        $this->auth->restrict();

        $this->load->helper('text');
        $this->load->model('m_member');
        $uri = $this->uri->uri_to_assoc(3);

        if (isset($uri['id'])) {
            $this->member_id = $uri['id'];
        }

        if (isset($uri['aktifasi'])) {
            $this->aktifasi = $uri['aktifasi'];
        }

        if (isset($_POST['in_cari'])) {
            $this->cari = $this->input->post('in_cari');
            $this->session->set_userdata('cari', $this->cari);
        }

        if ($this->session->userdata('cari')) {
            $this->cari = $this->session->userdata('cari');
        }

        if ($this->auth->cek_role_member() != '1') {
            redirect('homes');
        }
    }

    public function index($message = NULL) {
        $data['message'] = $message;
        //pengaturan untuk pagination
        $this->m_member->cari = $this->cari;

        $config['base_url'] = base_url() . 'index.php/members/index/page/';
        $config['total_rows'] = $this->m_member->get_row_all();
        $config['per_page'] = '20';

        $config['first_link'] = $this->lang->line('paging_first');
        $config['first_tag_open'] = '<li class="prev page">';
        $config['first_tag_close'] = '</li>';

        $config['last_link'] = $this->lang->line('paging_last');
        $config['last_tag_open'] = '<li class="next page">';
        $config['last_tag_close'] = '</li>';

        $config['next_link'] = $this->lang->line('paging_next');
        $config['next_tag_open'] = '<li class="next page">';
        $config['next_tag_close'] = '</li>';

        $config['prev_link'] = $this->lang->line('paging_prev');
        $config['prev_tag_open'] = '<li class="prev page">';
        $config['prev_tag_close'] = '</li>';

        $config['cur_tag_open'] = '<li class="active"><a href="">';
        $config['cur_tag_close'] = '</a></li>';

        $config['num_tag_open'] = '<li class="page">';
        $config['num_tag_close'] = '</li>';

        $config['uri_segment'] = 4;
        $this->m_member->num_pages = $config['per_page'];
        $this->m_member->offset = $this->uri->segment(4);
        $this->m_member->paginate = TRUE;
        $this->pagination->initialize($config);

        $members = $this->m_member->get();
        //$data = $this->auth->load_style();
//        $data['sm_member'] = 'active';
        $data['title'] = $this->lang->line('menu_member') . ' ' . $this->lang->line('webname');
        $data['members'] = $members;
        $data['paging'] = $this->pagination->create_links();
        $data['total'] = $config['total_rows'];

        if (!$this->uri->segment(4)) {
            $data['no_page'] = 0;
        } else {
            $data['no_page'] = $this->uri->segment(4);
        }

        $this->load->view('view_header', $data);
        $this->load->view('member_view', $data);
        $this->load->view('view_footer', $data);
    }

    public function view() {
        $this->session->unset_userdata('cari');

        redirect('members/');
    }

    public function add($message = NULL) {
        $this->form_validation->set_error_delimiters('<div class="alert alert-warning">', '</div>');
        $this->form_validation->set_rules('in_m_nama', $this->lang->line('nama'), 'trim|required|min_length[4]|xss_clean|callback__members_name_check');
        $this->form_validation->set_rules('in_m_pass', $this->lang->line('pass'), 'trim|required|min_length[4]|matches[in_m_pass2]');
        $this->form_validation->set_rules('in_m_pass2', $this->lang->line('pass2'), 'trim|required|min_length[4]');
        $this->form_validation->set_rules('in_m_namanya', $this->lang->line('namanya'), 'trim|required');
        $this->form_validation->set_rules('in_m_kelamin', $this->lang->line('kelamin'), 'trim|required');
        $this->form_validation->set_rules('in_m_email', $this->lang->line('email'), 'trim|required|valid_email');
        $this->form_validation->set_rules('in_m_role', $this->lang->line('role'), 'trim|required');
        $this->form_validation->set_rules('in_m_aktifasi', $this->lang->line('aktifasi'), 'trim|required');
        $this->form_validation->set_rules('in_m_status', $this->lang->line('status'), 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $members_role = $this->auth->load_role_member();
            $aktifs = array($this->lang->line('nonaktif'), $this->lang->line('aktif'));
            $members_kelamin = $this->auth->load_kelamin();

//            $data = $this->auth->load_style();
            //$data['sm_member'] = 'active';
            $data['title'] = $this->lang->line('menu_member') . ' ' . $this->lang->line('webname');
            $data['member_id'] = $this->member_id;
            $data['aktifs'] = $aktifs;
            $data['members_role'] = $members_role;
            $data['members_kelamin'] = $members_kelamin;
            $data['message'] = $message;

            $this->load->view('view_header', $data);
            $this->load->view('member_form', $data);
            $this->load->view('view_footer', $data);
        } else {
            $this->m_member->member_namanya = $this->input->post('in_m_namanya');
            $this->m_member->member_kelamin = $this->input->post('in_m_kelamin');
            $this->m_member->member_nama = $this->input->post('in_m_nama');
            $this->m_member->member_pass = $this->input->post('in_m_pass');
            $this->m_member->member_email = $this->input->post('in_m_email');
            $this->m_member->member_role = $this->input->post('in_m_role');
            $this->m_member->member_aktifasi = $this->input->post('in_m_aktifasi');
            $this->m_member->member_status = $this->input->post('in_m_status');
            $this->m_member->member_tanggal = date('Y-m-d');

            $this->m_member->member_oleh = $this->m_member->get_member_namenya($this->session->userdata('member_id'));
            $this->m_member->member_dirubah = date('Y-m-d H:i:s');

            $this->m_member->member_id = '';
            $this->m_member->save();

            $this->view();
            //redirect('members/');
        }
    }

    public function edit() {
        $this->form_validation->set_error_delimiters('<div class="alert alert-warning">', '</div>');
        $this->form_validation->set_rules('in_m_namanya', $this->lang->line('namanya'), 'trim|required');
        $this->form_validation->set_rules('in_m_kelamin', $this->lang->line('kelamin'), 'trim|required');
        $this->form_validation->set_rules('in_m_email', $this->lang->line('email'), 'trim|required|valid_email');
        $this->form_validation->set_rules('in_m_role', $this->lang->line('role'), 'trim|required');
        $this->form_validation->set_rules('in_m_aktifasi', $this->lang->line('aktifasi'), 'trim|required');
        $this->form_validation->set_rules('in_m_status', $this->lang->line('status'), 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $members_role = $this->auth->load_role_member();
            $aktifs = array($this->lang->line('nonaktif'), $this->lang->line('aktif'));
            $members_kelamin = $this->auth->load_kelamin();

//            $data = $this->auth->load_style();
            //$data['sm_member'] = 'active';
            $data['title'] = $this->lang->line('menu_member') . ' ' . $this->lang->line('webname');
            $data['member_id'] = $this->member_id;
            $data['aktifs'] = $aktifs;
            $data['members_role'] = $members_role;
            $data['members_kelamin'] = $members_kelamin;
            $data['members'] = $this->m_member->get_by_id($this->member_id);
            $data['message'] = $this->message;
            //print_r($data['message']);

            $this->load->view('view_header', $data);
            $this->load->view('member_form', $data);
            $this->load->view('view_footer', $data);
        } else {
//            $this->mod_member->member_nama	= $this->input->post('in_m_nama');
            $this->mod_member->member_pass = $this->input->post('in_m_pass');
            $this->m_member->member_namanya = $this->input->post('in_m_namanya');
            $this->m_member->member_kelamin = $this->input->post('in_m_kelamin');
            $this->m_member->member_email = $this->input->post('in_m_email');
            $this->m_member->member_role = $this->input->post('in_m_role');
            $this->m_member->member_aktifasi = $this->input->post('in_m_aktifasi');
            $this->m_member->member_status = $this->input->post('in_m_status');

            $this->m_member->member_oleh = $this->m_member->get_member_namenya($this->session->userdata('member_id'));
            $this->m_member->member_dirubah = date('Y-m-d H:i:s');

            $this->m_member->member_id = $this->member_id;
            $this->m_member->save();

            $this->view();
        }
    }

    public function delete() {
        $this->m_member->member_id = $this->member_id;
        $this->m_member->delete();

        $this->view();
    }

    public function _members_name_check($str) {
        if ($this->m_member->get_member_name($str)) {
            $this->form_validation->set_message('_members_name_check', 'Nama pengguna " ' . $str . ' " sudah digunakan.');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    public function members_aktifasi() {
        $this->m_member->member_id = $this->member_id;
        $this->m_member->member_aktifasi = $this->aktifasi;
        $this->m_member->set_aktifasi();

        redirect('members/view/');
    }

}

/* End of file members.php */
/* Location: ./application/controllers/members.php */